package stack;

import java.util.Stack;

/*
* 为解决
* */
public class MinStack {

    Stack<Integer> mainStack ;
    Stack<Integer> minStack;
    public MinStack() {
        mainStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x) {
        mainStack.push(x);
        Integer temp = 0;
        if (minStack.isEmpty()){
            minStack.push(x);
            temp = x;
        }else{
            temp = minStack.peek();
            if (temp >= x){
                minStack.push(x);
                temp = x;
            }else{
                minStack.push(temp);
            }
        }


    }

    public void pop() {
        mainStack.pop();
        minStack.pop();
    }


    public int top() {
        return mainStack.peek();
    }

    public int min() {
        return minStack.peek();
    }


    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);

        System.out.println(minStack.min());

        minStack.pop();


        System.out.println(minStack.top());
        System.out.println(minStack.min());
    }
}
/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */

